package dao.impl;

import dao.NoticeDao;
import domain.Notice;
import org.apache.commons.dbutils.QueryRunner;
import util.DataSourceUtils;
import util.JDBCUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;

public class NoticeDaoImpl implements NoticeDao {
    static Connection connection;
    static Statement statement = null;
    static {
        connection = JDBCUtil.getConnection();
        try {
            statement = connection.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Notice> findAllNotice() throws SQLException {
        List<Notice> listNotice = new LinkedList<>();
        String sql = "select * from notice";
        ResultSet res = statement.executeQuery(sql);
        while (res.next()) {
            Notice notice = new Notice();
            notice.setId(res.getInt(1));
            notice.setTitle(res.getString(2));
            notice.setDetails(res.getString(3));
            notice.setTime(res.getDate(4));
            listNotice.add(notice);
        }
        return listNotice;
    }

    @Override
    public int addNotice(Notice notice) throws SQLException {
        String sql= "insert into notice(title, details, n_time) values (?, ?, ?)";
        QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
        return runner.update(sql, notice.getTitle(), notice.getDetails(), notice.getTime());
    }

    @Override
    public Notice editNotice(String id) throws SQLException {
        if (id.isEmpty()) return null;
        String sql = "select * from notice where n_id = " + id;
        ResultSet res = statement.executeQuery(sql);
        Notice notice = null;
        while (res.next()) {
            notice = new Notice();
            notice.setId(res.getInt(1));
            notice.setTitle(res.getString(2));
            notice.setDetails(res.getString(3));
            notice.setTime(res.getDate(4));
        }
        return notice;
    }

    @Override
    public int updateNoticeByIde(Notice notice) throws SQLException {
        String sql = "update notice set title = ?, details = ? where n_id = ?";
        QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
        return runner.update(sql, notice.getTitle(), notice.getDetails(), notice.getId());
    }

    @Override
    public boolean removeNotice(Notice notice) throws SQLException {
        return false;
    }

    @Override
    public Notice getNewNotice() throws SQLException {
        String sql = "select * from notice where n_id = (select MAX(n_id) from notice)";
        ResultSet res = statement.executeQuery(sql);
        Notice notice = null;
        while (res.next()) {
            notice = new Notice();
            notice.setId(res.getInt(1));
            notice.setTitle(res.getString(2));
            notice.setDetails(res.getString(3));
            notice.setTime(res.getDate(4));
        }
        return notice;
    }
}
